const app = getApp()
const util = require('../../utils/util');
const config = require('../../common/config');

Page({
    data: {
        realName:'',
        mobile:'',
        cardNum:'',
        address:'',
        memberType:'',
        isLogin:false,
        sexCodes: [ "女","男","保密"],
        sexCodeIndex: 2
    },
    onLoad: function () {
        wx.setNavigationBarTitle({
            title: '个人信息'
        })
        var _this = this;
        var gender;
        if (app.globalData.userInfo != null) {
            this.setData({
                isLogin:app.globalData.isLogin
            })
        }

        const userId = wx.getStorageSync(config.USER_ID);
        if(userId == '' || userId == null){
            util.debug('userinfojs:获取微信用户会员信息失败:userId不存在，检查是否正常登录');
            return;
        }
        wx.request({
            url: config.COMMPT_REQPATH,
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method:'POST',
            data: {
                accessToken: '',
                refreshToken: '',
                serviceName: 'userService',
                methodName: 'getUserMemberInfo',
                reqData:JSON.stringify({
                    userId:userId
                })
            },
            success: function (res) {
                var resultData = res.data;
                if(resultData.code == 1){
                    util.debug('userinfojs:获取到会员用户详细信息:');
                    util.debug(resultData);

                    app.globalData.userInfo.realName = resultData.data.userMemberInfo.REAL_NAME==null?'':resultData.data.userMemberInfo.REAL_NAME;
                    app.globalData.userInfo.mobile = resultData.data.userMemberInfo.MOBILE==null?'':resultData.data.userMemberInfo.MOBILE;
                    app.globalData.userInfo.cardNum = resultData.data.userMemberInfo.CARD_NUMBER==null?'':resultData.data.userMemberInfo.CARD_NUMBER;
                    app.globalData.userInfo.address = resultData.data.userMemberInfo.ADDRESS==null?'':resultData.data.userMemberInfo.ADDRESS;
                    app.globalData.userInfo.memberType = resultData.data.userMemberInfo.MEMBER_TYPE==null?'':resultData.data.userMemberInfo.MEMBER_TYPE;
                    app.globalData.userInfo.gender = resultData.data.userMemberInfo.SEX==null?'':resultData.data.userMemberInfo.SEX;
                    app.globalData.userInfo.memberId = resultData.data.userMemberInfo.MEMBER_ID==null?'':resultData.data.userMemberInfo.MEMBER_ID;

                    _this.setData({
                        realName:app.globalData.userInfo.realName,
                        mobile:app.globalData.userInfo.mobile,
                        cardNum:app.globalData.userInfo.cardNum,
                        address:app.globalData.userInfo.address,
                        memberType:app.globalData.userInfo.memberType
                    })

                    var gender = app.globalData.userInfo.gender;
                    _this.setData({
                        sexCodeIndex:gender
                    })

                }else{
                    util.debug('userinfojs:获取会员用户详细信息失败:');
                    util.debug(resultData);
                }
            }
        })

    },
    toUserinfomanagePage:function (e) {
        wx.navigateTo({
            url: '../../pages/userinfomanage/index?userInfoType='+e.currentTarget.dataset.type
        })
    },
    toVeniclePage:function (e) {
        wx.navigateTo({
            url: '../../pages/venicle/index'
        })
    },
    toAddressesPage:function (e) {
        wx.chooseAddress({
            success: function (res) {
                util.debug(res.userName) //收货人姓名
                util.debug(res.postalCode) //邮编
                util.debug(res.provinceName) //国标收货地址第一级地址 省
                util.debug(res.cityName) //国标收货地址第二级地址 市
                util.debug(res.countyName)  //国标收货地址第三级地址 区
                util.debug(res.detailInfo) //详细收货地址信息
                util.debug(res.nationalCode) //收货地址国家码
                util.debug(res.telNumber) //收货人手机号码
            }
        })
    },
    toMoreinfoPage:function (e) {
        
    },
    toLogout:function (e) {
        var _this = this;
        wx.showActionSheet({
            itemList: ['退出登录'],
            itemColor:'#ff0000',
            success: function(res) {
                if(res.tapIndex == 0){
                    //清空本地缓存
                    wx.clearStorageSync();
                    app.globalData.userInfo = null;
                    //跳转登录页
                    wx.reLaunch({
                        url: '../../pages/login/index'
                    })
                }
            },
            fail: function(res) {
                util.debug("userinfojs:"+res.errMsg)
            }
        })
    },
    toModifyPassword:function (e) {
        
    },
    bindSexChange:function (e) {
        var _this  = this;
        _this.setData({
            sexCodeIndex: e.detail.value
        })
        const userId = wx.getStorageSync(config.USER_ID);
        wx.request({
            url: config.COMMPT_REQPATH,
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method:'POST',
            data: {
                accessToken: '',
                refreshToken: '',
                serviceName: 'userService',
                methodName: 'updateUserInfo',
                reqData:JSON.stringify({
                    userId:userId,
                    content:_this.data.sexCodeIndex,
                    type:'sex'
                })
            },
            success: function (res) {
                var resultData = res.data;
                if(resultData.code == 1){
                    util.debug("userinfojs:用户主表更新成功");
                    app.globalData.userInfo.gender= _this.data.sexCodeIndex
                }else{
                    util.debug("userinfojs:用户主表更新失败");
                }
            }
        })
    }
})